﻿using Power.Models;
using Power.View.Report;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Power.ViewModel.Report
{
    /// <summary>
    /// 分类工作量表
    /// </summary>
    public class ClassificationWorkLoadTableViewModel : ReportTableBase
    {
        public ClassifyTableData Data { get; set; }
        private ClassificationWorkLoadTable model;
        public ClassificationWorkLoadTableViewModel(ClassificationWorkLoadTable model)
        {
            this.model = model;
            Data = new ClassifyTableData()
            {
               PipeType = CreateEntity<ClassifyTableBaseValue>(3),
               PipeMate = CreateEntity<ClassifyTableBaseValue>(2),
               PipeDia = CreateEntity<ClassifyTableBaseValue>(5),
               DetecMethod = CreateEntity<ClassifyTableBaseValue>(3),
            };

            Init();
        }

        /// <summary>
        /// 动态创建测试数据
        /// </summary>
        /// <param name="subCount"></param>
        /// <returns></returns>
        public List<T> CreateEntity<T>(int count)
        {
            var list = new List<T>();

            for (int i = 0; i < count; i++)
            {
                var obj = Activator.CreateInstance(typeof(T));

                var type = obj.GetType();
                var props = type.GetProperties();
                foreach (var item in props)
                {
                    if (item.PropertyType == typeof(int))
                    {
                        item.SetValue(obj, 1);
                    }
                    else if (item.PropertyType == typeof(string))
                    {
                        item.SetValue(obj, "2");
                    }
                }
                list.Add((T)obj);
            }
            
            return list;
        }
        
        public void Init()
        {
            var color1 = new SolidColorBrush(Color.FromRgb(0, 0, 255));
            var color2 = new SolidColorBrush(Color.FromRgb(255, 255, 0));
            var color3 = new SolidColorBrush(Color.FromRgb(255, 102, 0));
            var color4 = new SolidColorBrush(Color.FromRgb(255, 0, 0));

            Row = 5; ;
            Column = Data.DetecMethod.Count + Data.PipeDia.Count + Data.PipeMate.Count + Data.PipeType.Count + 1;

            var table = this.model.table;

            for (int i = 0; i < Row; i++)
            {
                table.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50) });

            }
            for (int i = 0; i < Column; i++)
            {
                table.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(100) });
            }

            //画标题
            for (int i = 0; i < Row; i++)
            {
                for (int j = 0; j < Column; j++)
                {
                    DrawColumnSpanCell(table, i, j, "分类工作量表", Column, 0, 0);

                    DrawRowSpanCell(table, i, j, "分类", 2, 1, 0);

                    DrawColumnSpanCell(table, i, j, "管段类型", Data.PipeType.Count, 1, 1);
                    DrawColumnSpanCell(table, i, j, "管段材质", Data.PipeMate.Count, 1, Data.PipeType.Count + 1);
                    DrawColumnSpanCell(table, i, j, "管段直径", Data.PipeDia.Count, 1, Data.PipeType.Count + Data.PipeMate.Count + 1);
                    DrawColumnSpanCell(table, i, j, "检测方法", Data.DetecMethod.Count, 1, Data.PipeType.Count + Data.PipeMate.Count + Data.PipeDia.Count + 1);

                    if (j == 0)
                    {
                        DrawCell(table, i, j, "管段数量 段", 3, 0);
                        DrawCell(table, i, j, "检测长度 m", 4, 0);
                    }
                    else if (j > 0 && j < Data.PipeType.Count + 1)
                    {
                        DrawCell(table, i, j, Data.PipeType[j - 1].Name, 2, j);
                        DrawCell(table, i, j, Data.PipeType[j - 1].PipNum.ToString(), 3, j);
                        DrawCell(table, i, j, Data.PipeType[j - 1].Len.ToString(), 4, j);
                    }

                    else if (j > Data.PipeType.Count && j < Data.PipeType.Count + Data.PipeMate.Count + 1)
                    {
                        DrawCell(table, i, j, Data.PipeMate[j - Data.PipeType.Count -1].Name, 2, j);
                        DrawCell(table, i, j, Data.PipeMate[j - Data.PipeType.Count - 1].PipNum.ToString(), 3, j);
                        DrawCell(table, i, j, Data.PipeMate[j - Data.PipeType.Count - 1].Len.ToString(), 4, j);
                    }

                    else if (j > Data.PipeType.Count + Data.PipeMate.Count && j < Data.PipeType.Count + Data.PipeMate.Count + Data.PipeDia.Count + 1)
                    {
                        DrawCell(table, i, j, Data.PipeDia[j - Data.PipeType.Count - Data.PipeMate.Count - 1].Name, 2, j);
                        DrawCell(table, i, j, Data.PipeDia[j - Data.PipeType.Count - Data.PipeMate.Count - 1].PipNum.ToString(), 3, j);
                        DrawCell(table, i, j, Data.PipeDia[j - Data.PipeType.Count - Data.PipeMate.Count - 1].Len.ToString(), 4, j);
                    }

                    else
                    {
                        DrawCell(table, i, j, Data.DetecMethod[j - Data.PipeType.Count - Data.PipeMate.Count - Data.PipeDia.Count - 1].Name, 2, j);
                        DrawCell(table, i, j, Data.DetecMethod[j - Data.PipeType.Count - Data.PipeMate.Count - Data.PipeDia.Count - 1].PipNum.ToString(), 3, j);
                        DrawCell(table, i, j, Data.DetecMethod[j - Data.PipeType.Count - Data.PipeMate.Count - Data.PipeDia.Count - 1].Len.ToString(), 4, j);
                    }

                }
            }

            

            //补边框
            DrawBorder(table);
        }
    }
}
